import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/index',
    name: 'index',
    component: () => import(/* webpackChunkName: "about" */ '../views/index.vue'),
    meta:{title:"首页"},

    // 路由独享的守卫函数
    // beforeEnter: (to, from, next) => {
    //   // ...
    //   console.log("bbb")
    // },
    children:[
         {
           path:'/index/homepage',
           name:'homepage',
           meta:{title:"首页2"},
           component:()=>import('../views/index/homepage.vue')
         },
         {
          path:'/index/boom',
          name:'homepage1',
          meta:{title:"爆爆团"},
          component:()=>import('../views/index/boom.vue')
         },
         {
          path:'/index/order',
          name:'订单',
          meta:{title:"订单"},
          component:()=>import('../views/index/order.vue')
         },
         {
          path:'/index/mine',
          name:'我的',
          meta:{title:"我的"},
          component:()=>import('../views/index/mine.vue')
         },



    
         
        
            {
          
            path:'/index',
            redirect:'/index/homepage'
          
         },
         {
          path:'*',
          component: () => import('../views/index/NotFound.vue') 
         }
        ]
  },
  {
    // 第一种动态路由传参的时候需要用name
    // path:'/detail/:name',
    path:'/login',
    name:'login',
    meta:{title:"登录"},
    component:()=>import('../views/login.vue')
  },
  {
    // 第一种动态路由传参的时候需要用name
    // path:'/detail/:name',
    path:'/register',
    name:'register',
    meta:{title:"注册"},
    component:()=>import('../views/register.vue')
  },
  {
    // 第一种动态路由传参的时候需要用name
    // path:'/detail/:name',
    path:'/detail/:searchdata',
    name:'detail',
    meta:{title:"搜索列表"},
    component:()=>import('../views/detail.vue')
  },
  {
    path:'/xiangq/:id',
    name:'xiangq',
    meta:{title:'详情'},
    component: () => import('../views/xiangq.vue') 
   }, 
   {
    path:'/collect',
    name:'collect',
    meta:{title:'收藏'},
    component: () => import('../views/collect.vue') 
   }, 
   {
    path:'/search',
    name:'search',
    meta:{title:'搜索'},
    component: () => import('../views/search.vue') 
   }, 
   {
    path:'/buygoods',
    name:'buygoods',
    meta:{title:'收货地址'},
    component: () => import('../views/buygoods.vue') 
   },
   {
    path:'/address',
     name:'address',
    meta:{title: '收货地址'},
   component:()=>import('../views/MyShippingAddress.vue')
   },
   {
   path:'/addAddress',
   name:'addAddress',
    meta:{title: '新增地址'},
  component:()=>import('../views/AddAddress.vue')
  },
  {
  path:'/revise/:item',
  name:'revise',
   meta:{title: '修改地址'},
   component:()=>import('../views/ReviseAds.vue')
   },
  // 一级路由重定向
  {
    path:'/',
    redirect:'/index'
  },
  {
    path:'*',
    component: () => import('../views/index/NotFound.vue') 
   }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// 设置实例方法（导航守卫方法）


// 路由的全局前置守卫
router.beforeEach((to,from,next)=>{
    // console.log(to.meta.title)

    // to  新的路由对象
    // from 旧的路由对象
    // next  路由控制方法  调用该方法则允许路由跳转


    // 动态更新网页标题
     document.title = to.meta.title;

    //  获取localStorage中的登录凭证  
    var token = localStorage.getItem('token');
    if(to.path == '/index/order' || to.path == '/index/mine'){
      if(token){
        next()
      }else{
        next('../login')
      }
    }else{
        next()    //必须手动调用该函数，否则无法路由跳转
    }
    


    // 对于后台管理系统这种应用，只有登录页是可以随意访问的
  // if (to.path == '/login'){
  //   if(token ){
  //     next('/')

  //   }else{
  //     next();
  //   }
  // }else{
  //   if(token){
  //     next()
  //   }else{
  //      next('/login')
  //   }
  // }
  // next()









    
})

// 路由的全局后置守卫方法
router.afterEach((to,from)=>{
      // console.log("ddd")
})
export default router

